Imports DotNetPM.Business.Questionnaire

Partial Class Modules_Questionnaires_Controls_logDate
    Inherits System.Web.UI.UserControl
    Implements UserControls.ILogicUC

    Dim _question As Question

    Public Property Logic() As String Implements DotNetPM.Business.Questionnaire.UserControls.ILogicUC.Logic
        Get
            'Control Type   : 1
            'Operators
            'Choice
            'Date Type      :  -1 for No condition, 
            '                   0 for Spacific Date, 
            '                   1 to 5 for Date Range
            Dim sb As New StringBuilder
            sb.Append("1|")
            sb.Append(ddlOperators.SelectedIndex)
            If ddlDateType1.Visible Then
                If ddlDateType1.SelectedIndex = 2 Then
                    sb.Append("|")
                    sb.Append(txtChoices.Text)
                    sb.Append("|")
                Else
                    sb.Append("|")
                    sb.Append(calChoices.SelectedDate)
                    sb.Append("|")
                End If
                sb.Append(ddlDateType1.SelectedValue)
            Else
                sb.Append("|")
                sb.Append(calChoices.SelectedDate)
                sb.Append("|")
                sb.Append(ddlDateType0.SelectedValue)
            End If
            Return sb.ToString
        End Get
        Set(ByVal value As String)
            Dim val As String() = value.Split("|")
            Select Case val(3)
                Case "-1"
                    ddlDateType0.SelectedValue = "-1"
                Case "0"
                    ddlDateType0.SelectedValue = "0"
                    ddlOperators.SelectedIndex = val(1)
                    calChoices.SelectedDate = val(2)
                Case Else
                    ddlDateType1.SelectedValue = val(3)
                    If ddlDateType1.SelectedIndex = 3 Then
                        txtChoices.Text = val(2)
                    Else
                        calChoices.SelectedDate = val(2)
                    End If
            End Select
            AdjustControls()
        End Set
    End Property

    Public Property Question() As Question Implements DotNetPm.Business.Questionnaire.UserControls.ILogicUC.Question
        Get
            Return _question
        End Get
        Set(ByVal value As Question)
            _question = value
        End Set
    End Property

    Public Sub Reset() Implements DotNetPM.Business.Questionnaire.UserControls.ILogicUC.Reset
        ddlOperators.SelectedIndex = -1
        ddlDateType0.SelectedIndex = -1
        ddlDateType1.SelectedIndex = -1
        txtChoices.Text = String.Empty
        calChoices.SelectedDate = Now
        AdjustControls()
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        AdjustControls()
    End Sub

    Private Sub AdjustControls()
        Dim val As String() = Split(_question.Configuration, "||")
        If val(2) = "1" Then
            ' Date Range
            ddlDateType1.Visible = True
            ddlDateType0.Visible = False
            Select Case ddlDateType1.SelectedIndex
                Case 0
                    ddlOperators.Visible = False
                    calChoices.Visible = False
                    txtChoices.Visible = False
                    rangeChoices.Visible = False
                    reqChoices.Visible = False
                Case 1, 2
                    ddlOperators.Visible = False
                    calChoices.Visible = True
                    txtChoices.Visible = False
                    rangeChoices.Visible = False
                    reqChoices.Visible = True
                    reqChoices.ControlToValidate = "calChoices"
                    reqChoices.ErrorMessage = "Select a Date for this logic !"
                Case 3
                    ddlOperators.Visible = True
                    calChoices.Visible = False
                    txtChoices.Visible = True
                    rangeChoices.Visible = True
                    reqChoices.Visible = True
                    reqChoices.ControlToValidate = "txtChoices"
                    reqChoices.ErrorMessage = "Enter number of days for this logic !"
                Case Else
                    ddlOperators.Visible = True
                    calChoices.Visible = True
                    txtChoices.Visible = False
                    rangeChoices.Visible = False
                    reqChoices.Visible = True
                    reqChoices.ControlToValidate = "calChoices"
                    reqChoices.ErrorMessage = "Select a Date for this logic !"
            End Select
        Else
            ddlDateType1.Visible = False
            ddlDateType0.Visible = True
            Select Case ddlDateType0.SelectedIndex
                Case 0
                    ddlOperators.Visible = False
                    calChoices.Visible = False
                    txtChoices.Visible = False
                    rangeChoices.Visible = False
                    reqChoices.Visible = False
                Case 1
                    ddlOperators.Visible = True
                    calChoices.Visible = True
                    txtChoices.Visible = False
                    rangeChoices.Visible = False
                    reqChoices.Visible = True
                    reqChoices.ControlToValidate = "calChoices"
                    reqChoices.ErrorMessage = "Select a Date for this logic !"
            End Select
        End If
    End Sub

End Class
